<?php

namespace Rblog\Module\Admin\Controller;

class Artlist extends Index {
    private $_model;
    private $_artsort;
    private $_comment;
    
    public function __construct() {
        $this->_working = 'artlist';
        parent::__construct();
        $this->_model = \Rexzy::model( 'Arts' ); 
        $this->_artsort = \Rexzy::model( 'Artsort' ); 
        $this->_comment = \Rexzy::model( 'Comment' );
    }
    
    /**
     * 显示文章列表
     */
    public function show() {
        
        $length = 8;
        $condition = array();
        $index = false;
        
        if ( $this->request->isPost() ) {
            $condition['keyword'] = $this->request->input( 'keyword', 'p' );
            $condition['sort'] = $this->request->input( 'sort', 'p' );
            $page = 1;
            if ( ( $condition['keyword'] == '' ) && ( $condition['sort'] == 0 ) ){
                $index = true;
            }
            
        } else if ( $this->request->isGet() ) {
            if ( $this->request->input( 'keyword', 'g' ) != false ) {
                $condition['keyword'] = $this->request->input( 'keyword', 'g' );
            }
            if ( $this->request->input( 'sort', 'g' ) != false ) {
                $condition['sort'] = $this->request->input( 'sort', 'g' ); 
            }
            if ( $this->request->input( 'author', 'g' ) != false ) {
                $condition['author'] = $this->request->input( 'author', 'g' );
            }
            
            if ( $this->request->input('page', 'g' ) != false ) {
                $page = $this->request->input('page', 'g' );
            } else {
                $page = 1;
            }
            
            if ( count( $condition ) == 0 ) {
                $index = true;
            }
        }
        
        $start = ( $page - 1 ) * $length;
        $total = count( $this->_model->getList( $condition ) );
        $arts = $this->_model->getList($condition, $start, $length);
        $arts = $this->_comment->getEACNum( $arts );
        $listUrl = \Rexzy::url( 'admin/artlist' );
        
        if ( count( $arts ) != 0 ) {
            foreach ( $arts as $key => $val ) {
                $sorts = explode( ',', $val['sort'] );
                $sortmesg = array();
                foreach ( $sorts as $key1 => $val1 ) {
                    $name = $this->_artsort->select('name', "where sortid = $val1")['name'];
                    $sortmesg[] = "<a href='$listUrl?sort=$val1' class='line-btn'>$name</a>";
                }
                $arts[$key]['sortmesg'] = implode( ',', $sortmesg );
            }
        }
        
        $comUrl = \Rexzy::url( 'admin/artcomment' );
        $this->view->assign( array( 
            'arts' => $arts,
            'comUrl' => $comUrl,
            'listUrl' => $listUrl,
            'artsort' => $this->_artsort,
            'condition' => $condition,
            'length' => $length,
            'total' => $total,
            'page' => $page,
            'index' => $index,
            'editUrl' => \Rexzy::url('admin/artedit'),
            ) );
        $this->view->display();
    }
    
    /**
     * 删除文章
     */
    public function delete() {
        $post = $this->request->AllInput( 'p' );
        $artids = explode( ',', $post['value'] );
        $result = $this->_model->delArt( $artids );
        
        if ( $result > 0 ) {
            echo json_encode( array( 'delresult' => true ) );
        } else {
            echo json_encode( array( 'delresult', false ) );
        }
    }
}
